/**
 * 亿忆网活动功能JS
 * 
 * @author		布衣才子
 * @date		2012-09-20
 * @email		work.jerryliu@gmail.com
 * @qq			394969553
 * @version		v1.0
 * @copyright	copyright 2012-2014	YeeYi.com All Rights Reserved	
 */


/**
 * 
 * 服务器访问地址
 */
var apiHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";

var page = 1;

var tpage = 1;
/**
 * base64 加密对象初始化
 */
var b64 = new Base64();

/**
 * 网络请求函数
 * @param {Object} url  请求地址
 * @param {Object} callback	  回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 获取活动列表
 */
function  getActivityList(){
	uexWindow.toast("1","5","数据加载中…","0");
	var url = apiHost + '&ac=activity';
	page = 1;
	xmlHttp(url,showList);
}

/**
 * 活动列表回调函数，处理返回的数据，显示在界面上
 * @param {Object} items
 */
function showList(items){
	var outActivityObj = $("#forum_list");
	outActivityObj.html('');
	var j = 0;
	for(var i in items){
		j = j + 1;
		if(i == 0){
			var cssName = 'tip_first';
		}else{
			var cssName = 'tip';
		}
		
		
		var item = items[i];
		imgCache('p'+i,item.pic);
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'thread-touch\')" class="activeList" onclick="goActivityWindow('+item.htype+','+item.id+','+item.relatedid+')"><div class="ub-f1 ub ub-ver">';
		var pic = '<div class="thumbDiv"><p class="'+cssName+'">'+b64.decode(item.fname)+'&nbsp;</p><img id="p'+i+'" src="css/images/act_default.png" class="thumb"></div>';
		var trTitle = '<div class="detail"><div class="title">'+b64.decode(item.title)+'</div>';
		var trDesc = '<div class="desc">'+b64.decode(item.desc)+'</div>';
		var trOther = '<div class="other"><span>活动时间：'+b64.decode(item.dateline)+'</span></div>';
		var trFooter = '</div></div></div>';
		tr = trHeader+pic+trTitle+trDesc+trOther+trFooter;
		outActivityObj.append(tr);
	}
	uexWindow.closeToast();
	if(j == 5){
		$('.footer').show();
	}
}

/**
 * 图片缓存函数
 * @param {Object} rid	Dom ID
 * @param {Object} imgurl	网络图片路径
 */
function imgCache(rid,imgurl){
	//获取图片后缀
	var extArr = new Array();
	extArr = imgurl.split('.');
	var ext = extArr[extArr.length-1];
	zy_imgcache(rid,imgurl,imgurl,changeImgDom,err,null,ext);
}

/**
 * 缓存失败时的回调函数
 * @param {Object} rid	Dom ID
 * @param {Object} imgurl	网络图片路径
 */
function err(rid,imgurl){
	$('#'+rid).attr('src',imgurl);
}

/**
 * 缓存成功时的回调函数
 * @param {Object} rid
 * @param {Object} imgurl
 */
function changeImgDom(rid,imgurl){
	$('#'+rid).attr('src',imgurl);
}

/**
 * 回去下一页活动
 */
function  getMoreActivity(){
	uexWindow.toast("1","5","数据加载中…","0");
	page = page + 1;
	var url = apiHost + '&ac=activity&page='+page;
	$("#nextpage").text("数据加载中……");
	xmlHttp(url,showMoreList);
}

/**
 * 下一页活动列表回调函数
 * @param {Object} items
 */
function showMoreList(items){
	var outActivityObj = $("#forum_list");
	var start = (page - 1)*5;
	for(var i in items){
		var item = items[i];
		start = parseInt(start) + parseInt(i);
		imgCache('p'+start,item.pic);
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'thread-touch\')" class="activeList" onclick="goActivityWindow('+item.htype+','+item.id+','+item.relatedid+')"><div class="ub-f1 ub ub-ver">';
		var pic = '<div class="thumbDiv"><p class="tip">'+b64.decode(item.fname)+'&nbsp;</p><img id="p'+start+'" src="css/images/act_default.png" class="thumb"></div>';
		var trTitle = '<div class="detail"><div class="title">'+b64.decode(item.title)+'</div>';
		var trDesc = '<div class="desc">'+b64.decode(item.desc)+'</div>';
		var trOther = '<div class="other"><span>活动时间：'+b64.decode(item.dateline)+'</span></div>';
		var trFooter = '</div></div></div>';
		tr = trHeader+pic+trTitle+trDesc+trOther+trFooter;
		outActivityObj.append(tr);
	}
	uexWindow.closeToast();
	$("#nextpage").text("查看下5条");
}

/**
 * 查看活动细节
 */
function getActivity(){
	uexWindow.toast("1","5","数据加载中…","0");
	var aid = getLocalStorge('aid');
	if(!aid){
		uexWindow.alert('提示','活动ID丢失，请重新打开！','返回');
		uexWindow.close(-1);
	}else{
		var url = apiHost + '&ac=activityinfo&aid='+aid;
		xmlHttp(url,showActivity);
	}
}

/**
 * 显示活动具体内容
 * @param {Object} data
 */
function showActivity(data){
	var returnCode = data.code;
	if(returnCode == '200'){
		$('.activity_title').text(b64.decode(data.title));
		$('#fname').text(b64.decode(data.fname));
		$('#dateline').text(b64.decode(data.dateline));
		$('#desc').text(b64.decode(data.desc));
		localStorage.setItem('Atid',data.relatedid);
		localStorage.setItem('fid',data.fid);
		localStorage.setItem('typeid',data.typeid);
		var url = apiHost + '&ac=list&fid='+data.fid+'&typeid='+data.typeid;
		xmlHttp(url,showThreadList);
	}else if(returnCode == '404'){
		uexWindow.closeToast();
		uexWindow.alert('提示','未找到相关活动，请重试！','返回');
	}else{
		uexWindow.closeToast();
		uexWindow.alert('提示','未知错误，请重试！','返回');
	}
}

/**
 * 发布新帖
 */
function addNewThread(){
	var uid = getLocalStorge("uid");
	if(!uid){
		var confirmButtom = new Array();
		confirmButtom[0] = '登录';
		confirmButtom[1] = '返回';
		uexWindow.confirm('提示：','请登录后再回复！',confirmButtom);
		return false;
	}else{
		uexWindow.open('newthread','0','newthread.html','2','100%','100%','0');
	}
}


function ConfirmSuccess(opId,dataType,data){
	if (data == 0) {
		uexWindow.open('login','0','login.html','1','100%','100%','0');
	}
}

/**
 * 显示帖子列表
 * @param {Object} items
 */
function showThreadList(items){
	var outForumObj = $("#forum_list");
	var j = 0;
	for(var i in items){
		j = j + 1;
		var item = items[i];
		var hasImg = '';
		var parm = '';
		if(item.attachment == 'yes'){
			hasImg = ' <img src="css/images/attachimg.png">';		
		}
		
		if(item.replies >= 50){
			parm = " <img src='css/images/recommend.gif'>";
		}else if(item.views >= 2000){
			parm = " <img src='css/images/hot_3.gif'>";
		}else if(item.views >= 1200){
			parm = " <img src='css/images/hot_2.gif'>";
		}else if(item.views >= 700){
			parm = " <img src='css/images/hot_1.gif'>";
		}
		
		var tr = '';
		if(i == 0){
			var trHeader = '<div ontouchstart="zy_touch(\'thread-touch\')" class="forumList_sec" onclick="goThreadWindow('+item.tid+')"><div class="ub-f1 ub ub-ver">';
		}else{
			var trHeader = '<div ontouchstart="zy_touch(\'thread-touch\')" class="forumList" onclick="goThreadWindow('+item.tid+')"><div class="ub-f1 ub ub-ver">';
		}
		var trTitle = '<div class="title">'+b64.decode(item.subject)+hasImg+parm+'</div>';
		var trOtherInfo = '<div class="otherInfo"><span>'+b64.decode(item.dateline)+' '+b64.decode(item.author)+' 回复'+item.replies+'/查看'+item.views+'</span></div>';
		var trDesc = '<div class="desc">'+b64.decode(item.message)+'</div>';
		var trFooter = '</div></div>';
		tr = trHeader+trTitle+trOtherInfo+trDesc+trFooter;
		outForumObj.append(tr);
	}
	uexWindow.closeToast();
	if(j == 10){
		$('.footer').show();
	}
}

/**
 * 获取下一页帖子列表
 */
function getMoreThread(){
	var fid = getLocalStorge('fid');
	var typeid = getLocalStorge('typeid');
	if(!fid || !typeid){
		uexWindow.toast("0","5","板块ID丢失，请重新操作…","2000");
		return false;
	}
	tpage = tpage + 1;
	var url = apiHost + '&ac=list&fid='+fid+'&page='+tpage+'&typeid='+typeid;
	
	$("#nextpage").text("数据加载中……");
	xmlHttp(url,showMoreThread);
}

/**
 * 显示帖子列表
 * @param {Object} items
 */
function showMoreThread(items){
	var outForumObj = $("#forum_list");
	var j = 0;
	for(var i in items){
		j = j + 1;
		var item = items[i];
		var hasImg = '';
		var parm = '';
		if(item.attachment == 'yes'){
			hasImg = ' <img src="css/images/attachimg.png">';		
		}
		
		if(item.replies >= 50){
			parm = " <img src='css/images/recommend.gif'>";
		}else if(item.views >= 2000){
			parm = " <img src='css/images/hot_3.gif'>";
		}else if(item.views >= 1200){
			parm = " <img src='css/images/hot_2.gif'>";
		}else if(item.views >= 700){
			parm = " <img src='css/images/hot_1.gif'>";
		}
		
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'thread-touch\')" class="forumList" onclick="goThreadWindow('+item.tid+')"><div class="ub-f1 ub ub-ver">';
		var trTitle = '<div class="title">'+b64.decode(item.subject)+hasImg+parm+'</div>';
		var trOtherInfo = '<div class="otherInfo"><span>'+b64.decode(item.dateline)+' '+b64.decode(item.author)+' 回复'+item.replies+'/查看'+item.views+'</span></div>';
		var trDesc = '<div class="desc">'+b64.decode(item.message)+'</div>';
		var trFooter = '</div></div>';
		tr = trHeader+trTitle+trOtherInfo+trDesc+trFooter;
		outForumObj.append(tr);
	}
	uexWindow.closeToast();
	if(j < 10){
		$("#nextpage").text("已加载完所有活动贴");
	}else{
		$("#nextpage").text("查看下10条");
	}
}

/**
 * 帖子详情窗口跳转
 * @param {Object} tid
 */
function goThreadWindow(tid){
	if(tid == ''){
		uexWindow.alert("ERROR","未指定帖子ID,无法加载帖子内容！","返回");
	}else{
		localStorage.setItem("tid",tid);
		var threadUrl = "viewthread.html";
		uexWindow.open("threadWindow",0,threadUrl,2,"","",0x0);
	}
}

/**
 * 显示帖子型活动
 */
function showActThread(){
	var tid = getLocalStorge('Atid');
	if(!tid){
		uexWindow.alert('提示','参数缺失，请重试！','返回');
	}else{
		localStorage.setItem("tid",tid);
		localStorage.setItem('Atid','');
		var threadUrl = "activity_thread.html";
		uexWindow.open("athread",0,threadUrl,2,"","",4);
	}
}

/**
 * 返回顶部
 */
function pageScroll() {
    window.scrollBy(0,-50);
    scrolldelay = setTimeout('pageScroll()',1);
	var scrollTop = document.documentElement.scrollTop || window.pageYOffset;
    if(scrollTop==0){
         clearTimeout(scrolldelay);
    }
}


/**
 * 活动相关帖子页面跳转函数
 * @param {Object} ftype
 * @param {Object} id
 * @param {Object} tid
 */
function goActivityWindow(ftype,id,tid){
	if(ftype == 1){
		localStorage.setItem('aid',id);
		var activityURL = 'activity_list.html';
		uexWindow.open("activityWindow",0,activityURL,2,"","",4);
	}else{
		localStorage.setItem("tid",tid);
		var threadUrl = "activity_thread.html";
		uexWindow.open("athread",0,threadUrl,2,"","",4);
	}	
}

/**
 * HTML5 lcoalStage.getItem 的封装函数
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}